/**
 * 
 */
package com.net.shop.car.manager.ui.viewholder;

import android.graphics.Paint;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.net.shop.car.manager.R;
import com.net.shop.car.manager.api.model.Goods;
import com.net.shop.car.manager.utils.Constants;
import com.net.shop.car.manager.utils.StringUtils;

/**
 * @author Deve
 * 
 */
public class GoodViewHolder {
	/**
	 * 
	 */
	private CheckBox checkBox;
	/**
	 * 
	 */
	private TextView des;
	/**
	 * 
	 */
	private TextView price;
	/**
	 * 
	 */
	private TextView cheapPrice;
	/**
	 * 
	 */
	private final View view;
	
	/**
	 * 
	 */
	private boolean isCanPay;
	
	/**
	 * 
	 */
	private final OnGoodsAddedShopCarListener goodsAddedShopCarListener;

	/**
	 * 
	 */
	public GoodViewHolder(View view,OnGoodsAddedShopCarListener goodsAddedShopCarListener,boolean isCanPay) {
		this.view = view;
		this.isCanPay = isCanPay;
		this.goodsAddedShopCarListener = goodsAddedShopCarListener;
		checkBox = (CheckBox) view.findViewById(R.id.service_check);
		checkBox.setClickable(false);
		if(isCanPay) {
			checkBox.setVisibility(View.VISIBLE);
		} else {
			checkBox.setVisibility(View.GONE);
		}
		des = (TextView) view.findViewById(R.id.service_des);
		price = (TextView) view.findViewById(R.id.service_mon_last);
		cheapPrice = (TextView) view.findViewById(R.id.service_mon_current);

	}

	/**
	 * @param good
	 *            Goods
	 */
	public void setGood(final Goods good,boolean isChecked) {
		checkBox.setChecked(isChecked);
		view.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if(isCanPay) {
					checkBox.setChecked(!checkBox.isChecked());
					onGoodsAdded(good,checkBox.isChecked());
				}
			}
		});
		des.setText(good.getGoodName());
		if (good.getPrice().doubleValue() > 0) {
			price.setText(StringUtils.parseMoney(good.getPrice()));
			price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG ); 
		}
		if (good.getCheapPrice().doubleValue() > 0) {
			cheapPrice.setText(Constants.MONEY_CN + good.getCheapPrice());
		}
	}

	/**
	 * @param good Goods
	 * @param checked boolean
	 */
	private void onGoodsAdded(Goods good, boolean checked) {
		if(goodsAddedShopCarListener != null) {
			if (checked) {
				goodsAddedShopCarListener.onGoodsAdded(good);
			} else {
				goodsAddedShopCarListener.onGoodsRemoved(good);
			}
		}
	}

}
